package nl.fonfolio.view {
	import flash.display.Sprite;
	import flash.display.MovieClip;
	import flash.text.TextField;
	
	import caurina.transitions.Tweener;
	
	import flash.events.MouseEvent;
	import flash.events.Event;

	
	import nl.fonfolio.model.Model;
	import nl.fonfolio.controller.Controller;
	import nl.fonfolio.core.FonfolioEvent;	

	/**
	 * @author Paul
	 */
	public class Login extends Sprite {
		public var fonfolio:Sprite;
		public var email_txt : TextField;
		public var password_txt : TextField;
		public var message_txt : TextField;
		
		public var email_label : TextField;
		public var password_label : TextField;

		public var background:MovieClip;
		public var close_btn:MovieClip;
		public var doLogin_btn:MovieClip;

		private var model:Model;
		private var controller:Controller;
		
		public function Login(fonfolio:Sprite,model:Model,controller:Controller) 
		{
			this.fonfolio = fonfolio;
			this.model = model;
			this.controller = controller;
			
			this.model.addEventListener(FonfolioEvent.CONTROL_TYPE, this.update);
			this.fonfolio.stage.addEventListener(Event.RESIZE, this.onResizeStage);
			this.close_btn.addEventListener(MouseEvent.CLICK, this.close);
			this.doLogin_btn.addEventListener(MouseEvent.CLICK, this.doLogin);
			
			this.resize();
			this.x = this.fonfolio.stage.stageWidth;
		}
		
		private function doLogin(event : MouseEvent) : void {
			var objLogin:Object = new Object();
			objLogin.email = this.email_txt.text;
			objLogin.password = this.password_txt.text;
			this.controller.doLoginHandler(objLogin);
		}

		private function close(event:MouseEvent):void 
		{
			Tweener.addTween(this, {x:this.fonfolio.stage.stageWidth, time:0.5, transition:"easeInQuart"});
		}

		private function update(event : FonfolioEvent) : void 
		{
			switch (event.command){			
				case "LOGIN":
					Tweener.addTween(this, {x:this.fonfolio.stage.stageWidth - this.width, time:0.5, transition:"easeOutQuart"});
					//trace(this.model.getCurrentUser());
					var currentUser:Object = this.model.getCurrentUser();
					trace("return: " + currentUser + currentUser.error + currentUser.errorCode);
					
					if (currentUser.errorCode == false){
						this.message_txt.text = "Ingelogd";	
					} else 
					{
						this.message_txt.text = currentUser.error;	
						switch(currentUser.errorCode){
							case 101:
							break;
							case 102:
							break;
						}						
					}
					
					
				break;
			}
		}

		private function onResizeStage(e:Event):void 
		{
			this.resize();
		}

		private function resize():void 
		{
			this.background.height = this.fonfolio.stage.stageHeight;
			this.x = this.fonfolio.stage.stageWidth - this.width;
		}

		public function openLoginHandler() : void {
			Tweener.addTween(this, {x:this.fonfolio.stage.stageWidth - this.width, time:0.5, transition:"easeOutQuart"});
		}
	}
}
